package com.lpit.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lpit.constant.Constant;
import com.lpit.pojo.Message;
import com.lpit.service.MessageService;
import com.lpit.mapper.MessageMapper;
import com.lpit.utils.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author Administrator
* @description 针对表【message(聊天信息表)】的数据库操作Service实现
* @createDate 2023-06-08 11:06:01
*/
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message>
    implements MessageService{

    @Autowired
    private Tool tool;
    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /*推送消息给用户*/
    @Override
    public int pushNotification(Message message) {
        /*获取敏感词的集合*/
        List<String> sensitiveWordList = redisTemplate.opsForList().range(Constant.REDIS_KEY_SENSITIVE, 0, -1);
        /*判断为非空*/
        assert sensitiveWordList != null;
        /*排除敏感词，将敏感词修改成*号在传会修改后的值*/
        String newMessage = tool.eliminateSensitive(message.getMessageContent(), sensitiveWordList);
        /*把修改后的信息写入对象*/
        message.setMessageContent(newMessage);
        /*操作数据库插入数据*/
        return messageMapper.insert(message);
    }
}




